package org.meiling.test;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;

import javax.swing.JFrame;

import org.meiling.util.Sounds;
import org.meiling.util.Utils;

public class FirstMain extends JFrame implements MouseListener{
	boolean jx = false;
	public FirstMain(){
		this.setTitle("消灭星星");
		this.setIconImage(Utils.tubiao.getImage());
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		int locationX = (int)(dimension.getWidth()-Utils.WIDTH)/2;
		int locationY = (int)(dimension.getHeight()-Utils.HEIGHT)/2;
		this.setBounds(locationX,locationY,Utils.WIDTH,Utils.HEIGHT);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setResizable(false);
		this.setVisible(true);
		this.addMouseListener(this);
		File file = new File("./game.xml");
		if (file.isFile()) {
			jx = true;
		}
		Sounds.playBg();
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.drawImage(Utils.bgFirstPage.getImage(),0,0,this);
		if (jx==false) {
			g.drawImage(Utils.menu_start.getImage(),(Utils.WIDTH-Utils.menu_start.getImage().getWidth(null))/2,(Utils.HEIGHT-Utils.menu_start.getImage().getHeight(null))/2,this);
		}else{
			g.drawImage(Utils.jixu.getImage(),(Utils.WIDTH-Utils.menu_start.getImage().getWidth(null))/2,(Utils.HEIGHT-Utils.menu_start.getImage().getHeight(null))/2-40,this);
			g.drawImage(Utils.menu_start.getImage(),(Utils.WIDTH-Utils.menu_start.getImage().getWidth(null))/2,(Utils.HEIGHT-Utils.menu_start.getImage().getHeight(null))/2+60,this);
		}
		
	}
	
	
	public static void main(String[] args){
		new FirstMain();
	}

	
	
	@Override
	public void mouseClicked(MouseEvent e) {
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		if (jx==false) {
			Rectangle rect = new Rectangle((Utils.WIDTH-Utils.menu_start.getImage().getWidth(null))/2,(Utils.HEIGHT-Utils.menu_start.getImage().getHeight(null))/2,Utils.menu_start.getImage().getWidth(null),Utils.menu_start.getImage().getHeight(null));
			if (rect.contains(new Point(e.getX(),e.getY()))) {
				new Main().init();
				this.dispose();
			}
		}else{
			Rectangle rect = new Rectangle((Utils.WIDTH-Utils.menu_start.getImage().getWidth(null))/2,(Utils.HEIGHT-Utils.menu_start.getImage().getHeight(null))/2+60,Utils.menu_start.getImage().getWidth(null),Utils.menu_start.getImage().getHeight(null));
			Rectangle rect1 = new Rectangle((Utils.WIDTH-Utils.menu_start.getImage().getWidth(null))/2,(Utils.HEIGHT-Utils.menu_start.getImage().getHeight(null))/2-40,Utils.menu_start.getImage().getWidth(null),Utils.menu_start.getImage().getHeight(null));
			if (rect.contains(new Point(e.getX(),e.getY()))) {
				this.dispose();
				new Main().init();
			}else if (rect1.contains(new Point(e.getX(),e.getY()))) {
				this.dispose();
				new Main().returnInit();
			}
		}
		
		
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
